package jsu.lipengcheng.trafficnet.controlle.admin;

import jsu.lipengcheng.trafficnet.common.PageResult;
import jsu.lipengcheng.trafficnet.common.Result;
import jsu.lipengcheng.trafficnet.mapper.CarMapper;
import jsu.lipengcheng.trafficnet.pojo.Cars;
import jsu.lipengcheng.trafficnet.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/car")
public class AdmincarControlle {
    @Autowired
    private CarService carService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("{id}")
    public Result selectbyid(@PathVariable int id) {
        Cars cars= carService.selectbyid(id);
        return Result.success(cars);
    }

    /**
     * 分页查询用户车辆
     * @param page
     * @param pageSize
     * @param status
     * @param userid
     * @return
     */
    @GetMapping("/page")
    public Result selectall(int page,int pageSize,Integer status,Integer userid) {
        PageResult pageResult = carService.selectcarall(page, pageSize, status, userid);
        return Result.success(pageResult);
    }
    /**
     * 添加车辆
     * @param cars
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Cars cars) {
        carService.insert(cars);
        Set keys = redisTemplate.keys("carlist*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 修改车俩
     * @param cars
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Cars cars) {
        carService.update(cars);
        Set keys = redisTemplate.keys("carlist*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable int id) {
        carService.deletecar(id);
        Set keys = redisTemplate.keys("carlist*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @GetMapping()
    public Result selectall(){
        List<Cars> carsList= carService.selectall();
        return Result.success(carsList);
    }

}
